package cn.com.wangluotongxin.database.service.impl;

import cn.com.wangluotongxin.admin.eureka.AdminClient;
import cn.com.wangluotongxin.admin.eureka.form.AddSysMessageForm;
import cn.com.wangluotongxin.admin.model.AdminMerchantsDatabase;
import cn.com.wangluotongxin.admin.model.constants.DatabaseReleaseStatus;
import cn.com.wangluotongxin.admin.model.constants.DatabaseRunStatus;
import cn.com.wangluotongxin.admin.model.constants.MessageBusinessType;
import cn.com.wangluotongxin.admin.model.constants.MessageType;
import cn.com.wangluotongxin.database.mapper.AdminMerchantsDatabaseMapper;
import cn.com.wangluotongxin.database.service.IAdminMerchantsDatabaseService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网络通信-管理端-商户资源信息 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-04-28
 */
@Service
public class AdminMerchantsDatabaseServiceImpl extends ServiceImpl<AdminMerchantsDatabaseMapper, AdminMerchantsDatabase> implements IAdminMerchantsDatabaseService {

    @Autowired
    private AdminClient adminClient;

    @Override
    public void updateStatus(Integer id, DatabaseReleaseStatus releaseStatus, String message, Integer userId) {
        this.update(new LambdaUpdateWrapper<AdminMerchantsDatabase>()
                .set(AdminMerchantsDatabase::getReleaseStatus, releaseStatus.getCode())
                .set(AdminMerchantsDatabase::getReleaseMessage, message)
                .eq(AdminMerchantsDatabase::getId, id));

        AdminMerchantsDatabase merchantsDatabase = this.getById(id);
        StringBuilder content = new StringBuilder("发布数据库【"+ merchantsDatabase.getName() +"】：");
        if(merchantsDatabase.getReleaseStatus().equals(DatabaseReleaseStatus.FAIL.getCode())){
            content.append("失败<br>");
            content.append(message);
        }else {
            content.append("成功<br>");
            content.append(message);
        }
        AddSysMessageForm form = new AddSysMessageForm()
                .setType(MessageType.USER.getCode())
                .setBusinessType(MessageBusinessType.DATABASE.getCode())
                .setBusinessId(id)
                .setUserId(userId)
                .setTitle(MessageBusinessType.DATABASE.getTitle())
                .setContent(content.toString());
        adminClient.addSysMessage(form);
    }
}
